package com.rerun.task;

import com.rerun.common.util.CommonUtil;
import com.rerun.entity.MemberUser;
import com.rerun.entity.Resume;
import com.rerun.enums.MemberStatusEnum;
import com.rerun.service.IMemberUserService;
import com.rerun.service.IResumeService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户定时任务
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /9/29 16:17
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class MemberTask implements Job {
    private static final Logger log = LoggerFactory.getLogger(MemberTask.class);
    @Resource
    private IResumeService iResumeService;
    @Resource
    private IMemberUserService iMemberUserService;


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<Resume> resumeList = iResumeService.selectWhoNoAccount();
        List<MemberUser> users = new ArrayList<>();
        if (!resumeList.isEmpty()) {
            for (Resume resume : resumeList) {
                MemberUser user = new MemberUser();
                String phoneNumber = resume.getPhoneNumber();
                user.setPhone(phoneNumber);
                user.setCreateTime(new Date());
                user.setStatus(MemberStatusEnum.INACTIVE.getType());
//               默认密码 rerun2017
                String source = " yoyozhaopin2017";
                String password = new SimpleHash("MD5", source, phoneNumber, 1024).toString();
                user.setPassword(password);
                user.setSex(resume.getSex());
                users.add(user);
            }
        }
        if (!users.isEmpty()) {
            log.info("exec  new members");
            iMemberUserService.memberUserTask(users);
        } else {
            log.debug("{}  quartz: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> no member users data", CommonUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
        }
    }
}
